programming4us
           
 
 
Sharepoint

Sharepoint 2010 : Creating a .NET Connector in Visual Studio 2010 (part 1)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
4/24/2012 3:28:54 PM
One of the major strengths of BCS is how easy it is to integrate with almost any external data source. This section introduces the API and how to use Visual Studio 2010 to create a simple .NET connector. This connector example connects just to a data file but can be extended to connect to more complex data sources. .NET connectors can be discovered through the Discover function in SPD 2010.

1. Creating a Sample Flat File Data Source

First of all, we need a flat file with some data in it. Start by adding a .txt file on this path on the SharePoint server:

C:\Shared\zipcodes.txt

In real-life scenarios, it is likely to be on a file share, but for this example we keep it simple. In this example, the file contains city names and zip codes, separated with a comma and each name/zip pair on a new line. For this example, Danish zip codes are used. The file should look like this:

2750,Ballerup
1810,Frederiksberg
1720,Copenhagen West
...

2. Creating a .NET Assembly Connector Project in Visual Studio 2010

A .NET Assembly Connector is created as the project type Business Data Connectivity Model (BDC Model) in Visual Studio 2010. Open Visual Studio 2010, click File, select New, and then select Project. Choose the Business Data Connectivity Model project type, and name the project ZipCodesModel, as shown in Figure 1. Then click OK.

Figure 1. Selecting the Business Data Connectivity Model from the Projects window

Specify the SharePoint site where the model should be deployed (Figure 2), and click Finish.

Figure 2. Setting deployment site

Note that BDCM projects can be deployed only as a farm solution. This is because the model has to be deployed to the metadata store.

The BDCM project type creates a number of files automatically. These files shown in Solution Explorer (Figure 3) are the minimum required to make a new model and deploy it. If the BDCM is part of a third-party solution, it is likely that an alternative deployment method is used and the feature files and WSP package files can be removed.

Figure 3. Solution Explorer showing the BDC model

The required references to the SharePoint assemblies are automatically added. A feature for enabling and disabling the BDCM is included, and a WSP package containing the feature and model is added.

The BDC model itself contains a definition of the data source, connection information, and access layer information (query and return type information). The BDC model contains two classes: a class that defines the entity or external content type that this BDC model returns, and a class containing the code used to connect to the data source, query it, and return the entities it contains.

3. Creating an Entity (External Content Type)

The first real development task is to create the entity to be returned. It is very important to make sure that the entity contains the appropriate data to fulfill the business requirement for this .NET connector. To do this, the Entity1.cs file will be renamed ZipCodesEntity.cs and modified to map to the zip codes file.

To rename the file called Entity1.cs, right-click it in Solution Explorer and select Rename from the context menu. Type in ZipCodesEntity.cs, and click Yes when asked if all references should be renamed.

To modify the mapping, open the entity file by double-clicking ZipCodesEntity.cs. Delete the existing properties, and add two new properties called ZipCode and City. The class should now look as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ZipCodesModel.BdcModel1
{
   public partial class ZipCodesEntity
   {
       public string ZipCode { get; set; }
       public string City { get; set; }
   }
}

4. Creating an Entity Service Class

The entity service class is this example is used to query the data source—in this example, the zipcodes text file. The output of the entity service class will be one or all objects of the ZipCodesEntity class, depending on the operation performed.

Rename the Entity1Service.cs file to ZipCodesEntityService.cs as with the entity file, and click Yes when asked to update all references.

In Solution Explorer, double-click ZipCodesEntityService.cs to open it in code view.

As the data source is a text file, add the System.IO namespace, which contains the required classes for reading from the zipcodes text file.

using System.IO;

Some methods are required for the entity service class to function. The entity service class defines the Finder and Specific Finder methods used to return entities from the data source (zipcodes text file).

ReadList() is the Finder method of the BDC model. In this example, it should return all zipcode/city pairs from the data source. The method returns an IEnumerable generic collection of entities, which, in this case, is a collection of ZipCodesEntity objects. In this example, the objects are just created in memory, but advanced streaming schemes might be applied if the data source contains larger data sets. The ReadList() method can be implemented as shown in Listing 1.

Example 1. Implementation of the ReadList Method
public static IEnumerable<ZipCodesEntity> ReadList()
 {
     List<ZipCodesEntity> zipCodesEntityList = new List<ZipCodesEntity>();
    TextReader textReader = new StreamReader(@"C:\Shared\ZipCodes.txt");
    string zipCodeEntry;

     while ((zipCodeEntry = textReader.ReadLine()) != null)
     {
         ZipCodesEntity zipCodesEntity = new ZipCodesEntity();

         string[] entityData = zipCodeEntry.Split(',');

zipCodesEntity.ZipCode = entityData[0];
         zipCodesEntity.City = entityData[1];
         zipCodesEntityList.Add(zipCodesEntity);
      }

      textReader.Close();

      return zipCodesEntityList;
 }

The ReadItem(string zipCode) method defines the Specific Finder method for the BDC model. In this example, it returns a ZipCodesEntity object with the zipcode/city pair matching the zipcode argument. The ReadItem() method can be implemented as shown in Listing 2.

Example 2. Implementation of the ReadItem Method
public static ZipCodesEntity ReadItem(string zipCode)
 {
     foreach (ZipCodesEntity zipCodesEntity in ReadList())
     {
         if (zipCodesEntity.ZipCode == zipCode)
             return zipCodesEntity;
     }
     return null;
 }

It should now be possible to compile the assembly, which can be considered a .NET Assembly Connector at this point. Now the BDC model will be created, and the connection to the relevant data source, query capabilities, and return value type are defined.

Other -----------------
- Sharepoint 2007 : Associate a Workflow with a List or Library
- Sharepoint 2007 : Track the Progress of a Workflow
- Sharepoint 2007 : Start a Workflow
- Sharepoint 2007 : Create an Event with a Website
- Sharepoint 2007 : Create a Subsite
- Working with Search Page Layouts : Advanced Topics on Refinement Panel
- PerformancePoint Services 2010 (part 3) - Upgrading PerformancePoint Server 2007 & Migrating Content to Another SharePoint 2010 Location
- PerformancePoint Services 2010 (part 2) - Installing and Configuring PerformancePoint Services 2010
- PerformancePoint Services 2010 (part 1) - Understanding the Architecture of PerformancePoint Services 2010
- Working with Search Page Layouts : Modify the Search Results Presentation (part 2)
- Working with Search Page Layouts : Modify the Search Results Presentation (part 1)
- Working with Search Page Layouts : Applying a Branded Master Page to a Search Center
- SharePoint 2010 : Excel Services - User-Defined Functions
- SharePoint 2010 : Using the Excel Services REST API
- SharePoint 2010 : Social Sites: Providing a Structure for Collaborative Conversations
- SharePoint 2010 : Social Data: Enhancing Value with User Contributed Content
- Sharepoint 2010 : Excel Service - Demonstration Scenario (part 3)
- Sharepoint 2010 : Excel Service - Demonstration Scenario (part 2)
- Sharepoint 2010 : Excel Service - Demonstration Scenario (part 1)
- Working with Search Page Layouts : Adding Navigation to the Search Center (part 2)
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us